using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using MyLibrary; 

namespace MyWinFormApp
{
    public partial class Form1 : Form
    {
        private AlarmClock _alarmClock;
        private FileProcessor _fileProcessor;

        public Form1()
        {
            InitializeComponent();
            _alarmClock = new AlarmClock();
            _fileProcessor = new FileProcessor();

            _alarmClock.Tick += OnTick;
            _alarmClock.Alarm += OnAlarm;
        }

        private void OnTick(object sender, EventArgs e)
        {
            Invoke(new Action(() =>
            {
                lblTick.Text = "Tick: " + DateTime.Now.ToString("HH:mm:ss");
            }));
        }

        private void OnAlarm(object sender, EventArgs e)
        {
            Invoke(new Action(() =>
            {
                lblAlarm.Text = "Alarm: " + DateTime.Now.ToString("HH:mm:ss");
            }));
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "C# files (*.cs)|*.cs";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    var (lineCount, wordCount) = _fileProcessor.GetFileStatistics(filePath);
                    lblOriginalLines.Text = "Original Lines: " + lineCount;
                    lblOriginalWords.Text = "Original Words: " + wordCount;

                    var (formattedLineCount, formattedWordCount) = _fileProcessor.FormatFile(filePath);
                    lblFormattedLines.Text = "Formatted Lines: " + formattedLineCount;
                    lblFormattedWords.Text = "Formatted Words: " + formattedWordCount;

                    var wordFrequency = _fileProcessor.GetWordFrequency(filePath);
                    lstWordFrequency.Items.Clear();
                    foreach (var kvp in wordFrequency)
                    {
                        lstWordFrequency.Items.Add($"{kvp.Key}: {kvp.Value}");
                    }
                }
            }
        }

        private void btnSetAlarm_Click(object sender, EventArgs e)
        {
            TimeSpan alarmTime = TimeSpan.FromSeconds((double)numAlarmSeconds.Value);
            _alarmClock.SetAlarm(alarmTime);
        }

        private void btnStartClock_Click(object sender, EventArgs e)
        {
            _alarmClock.Start();
        }

        private void btnStopClock_Click(object sender, EventArgs e)
        {
            _alarmClock.Stop();
        }
    }
}